<?php

/**
 * Description of Util_Data
 *
 * @author Young Suk Ahn
 */
class Util_Datetime {
	
	const FORMAT_DATE_ISO = 'Y-m-d\TH:i:s';
	
	/**
	* Converts from ISO_8601
	* @param string $datetimeStr
	*/
	public static function parse_from_std($datetimeStr)
	{
		return Util_Datetime::parse_from($datetimeStr);
	}

	/**
	* converts from ISO_8601
	* @param string $datetimeStr
	*/
	public static function parse_from($datetimeStr, $format = Util_Datetime::FORMAT_DATE_ISO)
	{
		if ($datetimeStr == null)
			return null;
		$datetimeStr = substr($datetimeStr, 0, 19);
		$retval = DateTime::createFromFormat($format, $datetimeStr);

		return $retval;
	}

	/**
	* Format a date structure to a specific format
	* @param type $datetime
	* @return type 
	*/
	public static function format_std($datetime)
	{	
		return Util_Datetime::format($datetime);
	}

	public static function format($datetime, $format = Util_Datetime::FORMAT_DATE_ISO)
	{
		if ($datetime == null)
			return "";
		return $datetime->format($format);
	}

	public static function format_convert($datetimeStr, $fromFormat, $toFormat)
	{	
		if ($datetimeStr == null)
			return "";
		$datetime = Util_Datetime::parse_from($datetimeStr, $fromFormat);
		$retval = $datetime->format($toFormat);
		return $retval;
	}

	public static function time_diff($timestamp1, $timestamp2)
	{
		$s = $timestamp1-$timestamp2;
		$m=0;$hr=0;$d=0;$td="now"; 
		if($s>59) { 
			$m = (int)($s/60); 
			$s = $s-($m*60); // sec left over 
			$td = "$m min"; 
		} 
		if($m>59){ 
			$hr = (int)($m/60); 
			$m = $m-($hr*60); // min left over 
			$td = "$hr hr"; if($hr>1) $td .= "s"; 
			if($m>0) $td .= ", $m min"; 
		} 
		if($hr>23){ 
			$d = (int)($hr/24); 
			$hr = $hr-($d*24); // hr left over 
			$td = "$d day"; if($d>1) $td .= "s"; 
			if($d<3){ 
				if($hr>0) $td .= ", $hr hr"; if($hr>1) $td .= "s"; 
			} 
		} 
		return $td; 
	}

	public static function time_ago($datetimeStr)
	{
		$time = parse_from_std($datetimeStr);
		return Util_Datetime::time_diff(time(), $time->getTimestamp());
	}

	/**
	* From a string of format "yyyyMM" return year part
	* @param string $datetimeStr
	*/
	public static function get_year_from_yyyymm($str)
	{
		if ($str == null || strlen($str) < 4)
			return null;

		$year = intval( substr($str, 0, 4) );

		return $year;
	}

		/**
	* From a string of format "yyyyMM" return year part
	* @param string $datetimeStr
	*/
	public static function get_month_from_yyyymm($str)
	{
		if ($str == null || strlen($str) < 6)
			return null;

		$month = intval( substr($str, 4, 2) );

		return $month;
	}

	/**
	* From a string of format "yyyyMM" where the numbers are optional
	* extract the year and month respectively and returns as array
	* where first element is year and second month
	* @param string $datetimeStr
	*/
	public static function parse_yyyymm($str)
	{
		$year = Util_Datetime::get_year_from_yyyymm($str);
		$month = Util_Datetime::get_month_from_yyyymm($str);

		return array($year, $month);
	}

	/**
	* Given the array returned by parse_yearmonth, format into "Month yyyy"
	* e.g: 201203 yields March 2012
	* @param type $ym
	* @return string 
	*/
	public static function format_yyyymm($ym)
	{
		if ($ym == null)
			return "";

		if (is_string($ym))
			$ym = parse_yearmonth($ym);

		$retval = null;
		if ($ym[0] != null)
			$retval = $ym[0];
		if ($ym[1] != null) {
			$strmonth = date("F", mktime(0, 0, 0, ($ym[1] + 1))); 
			$retval = $strmonth.' '.$retval;
		}
		return $retval;
	}
	
	
}

